//
//  ESHelp.m
//  Eyeshot
//
//  Created by admin on 15/7/9.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#import "ESHelp.h"
#import "FMDatabase.h"


@interface ESHelp ()
@property (nonatomic, strong) NSString *filePath;
@end


@implementation ESHelp


+ (ESHelp *)helpManager
{
    static ESHelp *helpManager = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        helpManager = [[self alloc] init];
    });
    return helpManager;
}


- (void)createTabel
{
    
    NSFileManager * fileManager = [NSFileManager defaultManager];
    NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *filePath = [str stringByAppendingPathComponent:@"db.sqlite"];
    NSLog(@"5555555%@",filePath);
    self.filePath = filePath;
//    if ([fileManager fileExistsAtPath:filePath] == NO) {
        // create it
        FMDatabase * db = [FMDatabase databaseWithPath:filePath];
        if ([db open]) {
            
            
            NSString *sql = @"CREATE TABLE 'User' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'profile_image' TEXT, 'name' TEXT, 'passtime' TEXT, 'text' TEXT, 'cdn_img' TEXT, 'height' TEXT, 'width' TEXT,'user_id' TEXT,'love' TEXT, 'hate' TEXT, 'comment' TEXT, 'repost' TEXT)";
        
            
            BOOL res = [db executeUpdate:sql];//执行
            
            if (!res) {
                NSLog(@"error when creating db table");
            } else {
                NSLog(@"succ to creating db table");
            }
            [db close];
        } else {
            NSLog(@"error when open db");
        }
//    }
   
    
}


- (void)insertData:(ESNewPhotoModel *)model
{
    FMDatabase * db = [FMDatabase databaseWithPath:self.filePath];
    if ([db open]) {
        NSString * sql = @"insert into User (profile_image, name, passtime, text, cdn_img ,height,width, user_id, love, hate, comment, repost) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        BOOL res = [db executeUpdate:sql, model.profile_image,model.name,model.passtime,model.text,model.cdn_img,model.height,model.width,model.user_id,model.love,model.hate,model.comment,model.repost];
        
        if (!res) {
            NSLog(@"error to insert data");
        } else {
            NSLog(@"succ to insert data");
        }
        [db close];
    }
}


- (NSArray *)queryData
{
    FMDatabase * db = [FMDatabase databaseWithPath:self.filePath];
    
    NSMutableArray *muArr = [NSMutableArray arrayWithCapacity:0];

    if ([db open]) {
        NSString * sql = @"select * from User";
        FMResultSet * rs = [db executeQuery:sql];
        
        while ([rs next]) {
            ESNewPhotoModel *model = [[ESNewPhotoModel alloc] init];
    
            model.profile_image = [rs stringForColumn:@"profile_image"];
            model.name = [rs stringForColumn:@"name"];
             model.passtime = [rs stringForColumn:@"passtime"];
             model.text = [rs stringForColumn:@"text"];
             model.cdn_img = [rs stringForColumn:@"cdn_img"];
            model.height = [rs stringForColumn:@"height"];
            model.width = [rs stringForColumn:@"width"];
//            model.ID = [rs stringForColumn:@"ID"];
            model.user_id = [rs stringForColumn:@"user_id"];
             model.love = [rs stringForColumn:@"love"];
             model.hate = [rs stringForColumn:@"hate"];
             model.comment = [rs stringForColumn:@"comment"];
             model.repost = [rs stringForColumn:@"repost"];
           
            [muArr addObject:model];
        }
        [db close];
    }
    return muArr;
}



- (void)clearAll
{
    FMDatabase * db = [FMDatabase databaseWithPath:self.filePath];
    if ([db open]) {
        NSString * sql = @"delete from User";
        BOOL res = [db executeUpdate:sql];
        if (!res) {
            NSLog(@"error to delete db data");
        } else {
            NSLog(@"succ to deleta db data");
        }
        [db close];
    }
}

//- (IBAction)multithread:(id)sender {
//    FMDatabaseQueue * queue = [FMDatabaseQueue databaseQueueWithPath:self.dbPath];
//    dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
//    dispatch_queue_t q2 = dispatch_queue_create("queue2", NULL);
//    
//    dispatch_async(q1, ^{
//        for (int i = 0; i < 100; ++i) {
//            [queue inDatabase:^(FMDatabase *db) {
//                NSString * sql = @"insert into user (name, password) values(?, ?) ";
//                NSString * name = [NSString stringWithFormat:@"queue111 %d", i];
//                BOOL res = [db executeUpdate:sql, name, @"boy"];
//                if (!res) {
//                    debugLog(@"error to add db data: %@", name);
//                } else {
//                    debugLog(@"succ to add db data: %@", name);
//                }
//            }];
//        }
//    });
//    
//    dispatch_async(q2, ^{
//        for (int i = 0; i < 100; ++i) {
//            [queue inDatabase:^(FMDatabase *db) {
//                NSString * sql = @"insert into user (name, password) values(?, ?) ";
//                NSString * name = [NSString stringWithFormat:@"queue222 %d", i];
//                BOOL res = [db executeUpdate:sql, name, @"boy"];
//                if (!res) {
//                    debugLog(@"error to add db data: %@", name);
//                } else {
//                    debugLog(@"succ to add db data: %@", name);
//                }
//            }];
//        }
//    });
//}



/**
 @property (nonatomic, copy) NSString *profile_image;//头像图片
 @property (nonatomic, copy) NSString *name;//名字
 @property (nonatomic, copy) NSString *passtime;//显示的发布时间
 @property (nonatomic, copy) NSString *text;//发表内容
 @property (nonatomic, copy) NSString *cdn_img;//显示图片
 @property (nonatomic, copy) NSString *height;//图片高度
 @property (nonatomic, copy) NSString *width;//图片宽度
 @property (nonatomic, copy) NSString *ID;//id地址
 @property (nonatomic, copy) NSString *user_id;
 @property (nonatomic, copy) NSString *is_gif;//判断是否是gif类型图片
 @property (nonatomic, strong) NSArray *gifArr;//存放gif图片的数组
 @property (nonatomic, copy) NSString *love;//喜欢点赞的个数
 @property (nonatomic, copy) NSString *hate;//讨厌的个数
 @property (nonatomic, copy) NSString *comment;//评论的个数
 @property (nonatomic, copy) NSString *repost;//分享的个数
 
 */



@end
